import { createRouter, createWebHistory } from 'vue-router'
import { checkUserAuth } from '@/utils/auth'

import PCApp from '../App.vue'
import PCHome from '../views/Home.vue'
import PCLogin from '../views/Login.vue'
import PCHomeFilled from '../views/HomeFilled.vue'
import PCAnalysis from '../views/Analysis/Index.vue'
import PCAnalysisSuccess from '../views/Analysis/Success.vue'
import PCQuestion from '../views/Question.vue'
import PCPractice from '../views/Practice.vue'
import PCProfile  from '../views/Profile.vue'
import PCStudent  from '../views/Student/Index.vue'
import PCStudentAdd  from '../views/Student/Add.vue'
import PCStudentFirst  from '../views/Student/First.vue'
import PCAnalysisAnswerQuestions  from '../views/Analysis/AnswerQuestions.vue'
import PCAnalysisAnswerPaper  from '../views/Analysis/PaperView.vue'
import PCResultAnalysis  from '../views/Analysis/ResultAnalysis.vue'
import PCExercise  from '../views/Exercise/index.vue'
// 分享
import ShareAnalysis  from '../views/Share/ShareQuiz.vue'
import ShareAnalysisPaper  from '../views/Share/SharePaper.vue'
import ShareAnalysisQuestions  from '../views/Share/ShareAnswerQuestions.vue'
import ShareAnalysisResult  from '../views/Share/ShareResult.vue'
// 404
import PCNotFound from '../views/NotFound.vue'

const routes = [
  {
    path: '/',
    component: PCApp,
    children: [
      {
        path: '',
        redirect: '/home',
        meta: { requiresAuth: false }
      },
      {
        path: 'login',
        name: 'login',
        component: PCLogin,
        meta: { requiresAuth: false }
      },
      {
        path: 'home',
        name: 'home',
        component: PCHome,
        meta: { requiresAuth: false }
      },
      {
        path: 'share/analysis',
        name: 'shareAnalysis',
        component: ShareAnalysis,
        meta: { requiresAuth: false }
      },
      {
        path: 'share/peper',
        name: 'shareAnalysisPaper',
        component: ShareAnalysisPaper,
        meta: { requiresAuth: false }
      },
      {
        path: 'share/answerQuestions',
        name: 'shareAnalysisQuestions',
        component: ShareAnalysisQuestions,
        meta: { requiresAuth: false }
      },
      {
        path: 'share/result',
        name: 'shareAnalysisResult',
        component: ShareAnalysisResult,
        meta: { requiresAuth: false }
      },
      {
        path: 'homeFilled',
        name: 'homeFilled',
        component: PCHomeFilled,
      },
      {
        path: 'analysis',
        name: 'analysis',
        component: PCAnalysis,
      },
      {
        path: 'analysis/success',
        name: 'analysisSuccess',
        component: PCAnalysisSuccess,
      },
      {
        path: 'analysis/answerQuestions',
        name: 'analysisAnswerQuestions',
        component: PCAnalysisAnswerQuestions,
      },
      {
        path: 'analysis/paper',
        name: 'analysisAnswerPaper',
        component: PCAnalysisAnswerPaper,
      },
      {
        path: '/result',
        name: 'resultAnalysis',
        component: PCResultAnalysis,
      },
      {
        path: '/exercise',
        name: 'exercise',
        component: PCExercise,
      },
      {
        path: 'question',
        name: 'question',
        component: PCQuestion,
      },
      {
        path: 'practice',
        name: 'practice',
        component: PCPractice,
      },
      {
        path: 'student',
        name: 'student',
        component: PCStudent,
      },
      {
        path: 'student/add',
        name: 'studentAdd',
        component: PCStudentAdd,
      },
      {
        path: 'student/first',
        name: 'studentFirst',
        component: PCStudentFirst,
      },
      {
        path: 'profile',
        name: 'profile',
        component: PCProfile,
      },
      {
        path: '/404',
        name: '404',
        component: PCNotFound
      },
    ]
  }, 
  // 将匹配所有路径并重定向到404页面
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach(async (to, from, next) => {
  // 如果是访问登录页，直接放行
  if (to.path === '/login' || to.path === '/mobile/login') {
    next()
    return
  }
  
  // 如果是首页，直接放行
  if (to.path === '/home' || to.path === '/mobile/home') {
    next()
    return
  }

  // 检查用户是否已登录
  if (!checkUserAuth()) {
    // 未登录，跳转到对应端的登录页
    // const isMobile = to.path.startsWith('/mobile')
    // next({
    //   path: isMobile ? '/mobile/login' : '/login',
    //   query: { redirect: to.fullPath }
    // })
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
    return
  }

  // 已登录，允许访问
  next()
})

export default router
